package com.bonc.modules.item.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jinlong
 * @date 2021/8/6
 */
public class ChartDateUtils {

    /**
     * 获取指定日期的前30天
     *
     * @param dateStr 指定日期("yyyy-MM-dd")
     * @return
     */
    public static List<ChartDate> get30Days(String dateStr) {
        int totalDays = 30;
        List<ChartDate> resultList = new ArrayList<>();
        Date endDate = DateUtil.parse(dateStr, "yyyy-MM-dd");
        for (int i = 0; i < totalDays; i++) {
            resultList.add(0, new ChartDate(DateUtil.offsetDay(endDate, -i).getTime(), ChartDateTypeEnum.DAY));
        }
        return resultList;
    }

    /**
     * 获取指定月份的前12个月
     *
     * @param monthStr 指定月份("yyyy-MM")
     * @return
     */
    public static List<ChartDate> get12Month(String monthStr) {
        int totalMonth = 12;
        List<ChartDate> resultList = new ArrayList<>();
        DateTime endMonth = DateUtil.endOfMonth(DateUtil.parse(monthStr, "yyyy-MM"));
        for (int i = 0; i < totalMonth; i++) {
            resultList.add(0, new ChartDate(DateUtil.offsetMonth(endMonth, -i).getTime(), ChartDateTypeEnum.MONTH));
        }
        return resultList;
    }

    /**
     * 获取指定季度的前4个季度
     *
     * @param quarterStr 指定季度("yyyyMMdd")
     * @return
     */
    public static List<ChartDate> get4Quarter(String quarterStr) {
        int totalMonth = 4;
        List<ChartDate> resultList = new ArrayList<>();
        DateTime endQuarter = DateUtil.parse(quarterStr, "yyyyMMdd");
        for (int i = 0; i < totalMonth; i++) {
            resultList.add(0, new ChartDate(DateUtil.offsetMonth(endQuarter, -i * 3).getTime(), ChartDateTypeEnum.QUARTER));
        }
        return resultList;
    }


    /**
     * 获取指定年份的前10个年
     *
     * @param yearStr 指定年份("yyyy")
     * @return
     */
    public static List<ChartDate> get10Year(String yearStr) {
        int totalMonth = 10;
        List<ChartDate> resultList = new ArrayList<>();
        DateTime endYear = DateUtil.endOfYear(DateUtil.parse(yearStr, "yyyy"));
        for (int i = 0; i < totalMonth; i++) {
            resultList.add(0, new ChartDate(DateUtil.offsetMonth(endYear, -i * 12).getTime(), ChartDateTypeEnum.YEAR));
        }
        return resultList;
    }


    public static List<ChartDate> getRecent(String dateStr, ChartDateTypeEnum dateType) {
        switch (dateType) {
            // 月
            case MONTH:
                return get12Month(dateStr);
            //季度
            case QUARTER:
                return get4Quarter(dateStr);
            //年
            case YEAR:
                return get10Year(dateStr);
            //日
            case DAY:
            default:
                return get30Days(dateStr);

        }
    }

    /**
     * 获取日
     *
     * @param dateStr
     * @return
     */
    public static ChartDate getBeginOfDay(String dateStr) {
        Date date = DateUtil.parse(dateStr, "yyyy-MM-dd");
        return new ChartDate(DateUtil.beginOfDay(date).getTime(), ChartDateTypeEnum.DAY);
    }

    /**
     * 获取月最后一天
     *
     * @param dateStr
     * @return
     */
    public static ChartDate getEndOfMonth(String dateStr) {
        Date date = DateUtil.parse(dateStr, "yyyy-MM");
        return new ChartDate(DateUtil.beginOfDay(DateUtil.endOfMonth(date)).getTime(), ChartDateTypeEnum.MONTH);
    }

    /**
     * 获取季度最后一天
     *
     * @param dateStr
     * @return
     */
    public static ChartDate getEndOfQuarter(String dateStr) {
        Date date = DateUtil.parse(dateStr, "yyyyMMdd");
        return new ChartDate(DateUtil.beginOfDay(DateUtil.endOfQuarter(date)).getTime(), ChartDateTypeEnum.QUARTER);
    }

    /**
     * 获取年最后一天
     *
     * @param dateStr
     * @return
     */
    public static ChartDate getEndOfYear(String dateStr) {
        Date date = DateUtil.parse(dateStr, "yyyy");
        return new ChartDate(DateUtil.beginOfDay(DateUtil.endOfYear(date)).getTime(), ChartDateTypeEnum.YEAR);
    }


    public static ChartDate getEnd(String dateStr, ChartDateTypeEnum dateType) {
        switch (dateType) {
            // 月
            case MONTH:
                return getEndOfMonth(dateStr);
            //季度
            case QUARTER:
                return getEndOfQuarter(dateStr);
            //年
            case YEAR:
                return getEndOfYear(dateStr);
            //日
            case DAY:
            default:
                return getBeginOfDay(dateStr);

        }
    }

    public static void main(String[] args) {
        System.out.println(getRecent("2021-03-31", ChartDateTypeEnum.DAY));
        System.out.println(getRecent("2021-03", ChartDateTypeEnum.MONTH));
        System.out.println(getRecent("20210331", ChartDateTypeEnum.QUARTER));
        System.out.println(getRecent("2021", ChartDateTypeEnum.YEAR));


        System.out.println(getEnd("2021-03-31", ChartDateTypeEnum.DAY).getTime());
        System.out.println(getEnd("2021-03", ChartDateTypeEnum.MONTH).getTime());
        System.out.println(getEnd("20210331", ChartDateTypeEnum.QUARTER).getTime());
        System.out.println(getEnd("2021", ChartDateTypeEnum.YEAR).getTime());
    }

}
